package test.gateway.b.remote;

import com.netflix.hystrix.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class QueryTokenCommand extends HystrixCommand<String> {
    private IdcardService idcardService;
 
    public QueryTokenCommand(IdcardService idcardService) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("orderService"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("queryByOrderId"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        .withCircuitBreakerRequestVolumeThreshold(10)//至少有10个请求，熔断器才进行错误率的计算
                        .withCircuitBreakerSleepWindowInMilliseconds(5000)//熔断器中断请求5秒后会进入半打开状态,放部分流量过去重试
                        .withCircuitBreakerErrorThresholdPercentage(50)//错误率达到50开启熔断保护
                        .withExecutionTimeoutEnabled(true))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties
                        .Setter().withCoreSize(10)));
        this.idcardService = idcardService;
    }
 
    @Override
    protected String run() {
        return idcardService.token();
    }
 
    @Override
    protected String getFallback() {
        return "fail";
    }
}